/**
 * 
 */
package com.adinnc;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TableLayout;
import android.widget.TextView;

import com.adinnc.Util.StringUtils;
import com.adinnc.vo.JourneyDetails;
import com.adinnc.vo.PassengerDetails;
import com.haarman.supertooltips.ToolTip;
import com.haarman.supertooltips.ToolTipRelativeLayout;
import com.haarman.supertooltips.ToolTipView;
import com.haarman.supertooltips.ToolTipView.OnToolTipViewClickedListener;

/**
 * @author adi
 * 
 */
public class CheckPNRStatusActivity extends Activity implements View.OnClickListener,
		OnToolTipViewClickedListener {

	private TextView trainDate;
	private TextView trainTo;
	private TextView trainFrom;
	private TextView trainName;
	private TextView trainNo;
	private TextView chartStatus;
	private JourneyDetails jDetails;
	private ToolTipView chartStatusToolTipView;
	private ToolTipRelativeLayout toolTipRelativeLayout;
	private ImageView mActionbarBack;
	private ImageView mActionbarShare;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// checking the PNR status.
		super.onCreate(savedInstanceState);
		setContentView(R.layout.traindetails_layout);

		trainDate = (TextView) findViewById(R.id.trainDate_value);
		trainFrom = (TextView) findViewById(R.id.trainFrom_value);
		trainName = (TextView) findViewById(R.id.trainName_value);
		trainNo = (TextView) findViewById(R.id.trainNo_value);
		trainTo = (TextView) findViewById(R.id.trainTo_value);
		chartStatus = (TextView) findViewById(R.id.chart_prepared);
		
		mActionbarBack = (ImageView) findViewById(R.id.actionbar_back_train_status);
		mActionbarBack.setOnClickListener(this);
		
		mActionbarShare = (ImageView) findViewById(R.id.actionbar_share_train_status);
		mActionbarShare.setOnClickListener(this);

		toolTipRelativeLayout = (ToolTipRelativeLayout) findViewById(R.id.activity_main_tooltipRelativeLayout);

		findViewById(R.id.chartstatus_tooltip_info).setOnClickListener(this);
		Intent intent = getIntent();
		jDetails = (JourneyDetails) intent
				.getSerializableExtra("com.adinnc.rnp.journeyDetail");
		handleJourneyDetails();
	}


	private void handleJourneyDetails() {
		trainDate.setText(jDetails.trainDate);
		trainFrom.setText(jDetails.from);
		trainName.setText(jDetails.trainName);
		trainNo.setText(jDetails.trainNumber);
		trainTo.setText(jDetails.to);
		chartStatus.setText(jDetails.chartStatus);

		drawPassengersDetails();
	}

	public void drawPassengersDetails() {
		int rowCount = jDetails.allPassengers.size();
		Log.d("Fill Table", "rowCount = " + rowCount);
		TableLayout table = (TableLayout) this
				.findViewById(R.id.passenger_details);
		for (PassengerDetails p : jDetails.allPassengers) {
			// Create table from xml layout file using layout inflater:
			fillPassengerDetails(table, p);
		}
	}

	public void fillPassengerDetails(TableLayout table, PassengerDetails pD) {
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View fullRow = inflater.inflate(R.layout.passenger_row, null, false);
		TextView passgenerId = (TextView) fullRow
				.findViewById(R.id.passenger_id);
		passgenerId.setText(pD.sno);
		TextView bStatus = (TextView) fullRow.findViewById(R.id.booking_status);
		bStatus.setText(pD.bookingStatus);
		TextView currentStatus = (TextView) fullRow
				.findViewById(R.id.current_status);
		currentStatus.setText(pD.currentStatus);
/*		ImageView statusIcon = (ImageView) fullRow
				.findViewById(R.id.status_icon);
		
		// logic to show the icon based on the status of the ticket
		String uri = StringUtils.chartStatus(pD.currentStatus).toString();
		statusIcon.setImageDrawable(getResources().getDrawable(
									getResources().getIdentifier("drawable/" + uri, "drawable", getPackageName())));
*/		table.addView(fullRow);
	}

	public void onToolTipViewClicked(ToolTipView toolTipView) {
		if (chartStatusToolTipView == toolTipView) {
			chartStatusToolTipView = null;
		}
	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.chartstatus_tooltip_info:
			if (chartStatusToolTipView == null) {
				addBlueToolTipView();
			} else {
				chartStatusToolTipView.remove();
				chartStatusToolTipView = null;
			}
			break;
		case R.id.actionbar_back_train_status:
	         finish();
	        break;
		case R.id.actionbar_share_train_status:
			 Intent i = new Intent(Intent.ACTION_SEND);
             i.setType("text/plain");
             i.putExtra(Intent.EXTRA_SUBJECT, jDetails.chartStatus);
             i.putExtra(Intent.EXTRA_TEXT, StringUtils.concatObject(jDetails.allPassengers) +"\n"+ jDetails.chartStatus);
             startActivity(Intent.createChooser(i, getString(R.string.share_pnrstatus)));
             break;
		}
	}
	
	private void addBlueToolTipView() {
		chartStatusToolTipView = toolTipRelativeLayout.showToolTipForView(
                new ToolTip()
                        .withContentView(LayoutInflater.from(this).inflate(R.layout.custom_tooltip, null))
                        .withColor(getResources().getColor(R.color.holo_green))
                        .withAnimationType(ToolTip.ANIMATIONTYPE_FROMMASTERVIEW),
                findViewById(R.id.chartstatus_tooltip_info));
        chartStatusToolTipView.setOnToolTipViewClickedListener(this);
    }
}
